package com.gree.scmservice.service.impl;

import com.gree.common.entity.SysDictData;
import com.gree.common.entity.TablePageData;
import com.gree.common.utils.DictUtils;
import com.gree.common.utils.StringUtils;
import com.gree.framework.utils.SecurityUtils;
import com.gree.scmservice.dao.SysDictDataDao;
import com.gree.scmservice.service.SysDictDataService;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

@Service("SysDictDataService")
public class SysDictDataServiceImpl implements SysDictDataService {

    @Autowired
    private SysDictDataDao sysDictDataDao;

    @Autowired
    private Dao dao;

    /**
     * 项目启动时，初始化字典到缓存
     */
    @PostConstruct
    public void init()
    {
        List<Record> dictTypeList = sysDictDataDao.selectDictTypeAll();
        for (Record dictType : dictTypeList)
        {
            List<SysDictData> dictDatas = sysDictDataDao.selectDictDataByType(dictType.getString("dict_type"));
            DictUtils.setDictCache(dictType.getString("dict_type"), dictDatas);
        }
    }

    @Override
    public TablePageData<SysDictData> selectDictDataList(SysDictData dictData) {
        TablePageData<SysDictData> sysDictDataTablePageData = sysDictDataDao.selectDictDataList(dictData);
        return sysDictDataTablePageData;
    }

    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        return null;
    }

    @Override
    public SysDictData selectDictDataById(Long dictCode) {
        return sysDictDataDao.fetchSysDictById(dictCode);
    }

    @Override
    public int deleteDictDataByIds(Long[] dictCodes) {
        int count = sysDictDataDao.deleteSysDictDataByDictCodes(dictCodes);
        if (count > 0)
        {
            DictUtils.clearDictCache();
        }
        return count;
    }

    @Override
    public int insertDictData(SysDictData sysDictData) {
        sysDictData.setCreate_by(SecurityUtils.getUser().getUsername());
        sysDictData.setCreate_time(new Date());
        SysDictData result = sysDictDataDao.insertSysDictData(sysDictData);
        if (result != null)
        {
            DictUtils.clearDictCache();
        }
        return 1;
    }

    @Override
    public int updateDictData(SysDictData sysDictData) {
        SysDictData sysDictData1 = sysDictDataDao.fetchSysDictById(sysDictData.getDict_code());
        sysDictData.setCreate_by(sysDictData1.getCreate_by());
        sysDictData.setCreate_time(sysDictData1.getCreate_time());
        sysDictData.setUpdate_by(SecurityUtils.getUser().getUsername());
        sysDictData.setUpdate_time(new Date());
        int result = sysDictDataDao.updateSysDictData(sysDictData);
        if (result > 0)
        {
            DictUtils.clearDictCache();
        }
        return result;
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType)
    {
        List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
        if (StringUtils.isNotEmpty(dictDatas))
        {
            return dictDatas;
        }
        dictDatas = sysDictDataDao.selectDictDataByType(dictType);
        if (StringUtils.isNotEmpty(dictDatas))
        {
            DictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }

    /**
     * 清空缓存数据
     */
    @Override
    public void clearCache()
    {
        DictUtils.clearDictCache();
    }
}
